<?php

namespace services\storage;

use services\SystemConfigService;
use Guzzle\Http\EntityBody;
use Qcloud\Cos\Client;
use think\facade\Cache;

/**
 * TODO 腾讯云COS文件上传
 * Class COS
 */
class COS
{
    protected static $accessKey;

    protected static $secretKey;

    protected static $auth = null;

    //TODO 空间域名 Domain
    protected static $uploadUrl;

    //TODO 存储空间名称  公开空间
    protected static $storageName;

    //TODO COS使用  所属地域
    protected static $storageRegion;

 

    protected static $static_path;

    /**
     * TODO 初始化
     * @return null|Client
     * @throws \Exception
     */
    protected static function autoInfo()
    {
        self::$accessKey     = freshCache('storage_info.storage_cos_access_key');
        self::$secretKey     = freshCache('storage_info.storage_cos_secret_key');
        self::$uploadUrl     = freshCache('storage_info.storage_cos_domain');
        self::$storageName   = freshCache('storage_info.storage_cos_bucket');
        self::$storageRegion = freshCache('storage_info.storage_cos_region');
        self::$static_path   = freshCache('storage_info.storage_cos_static_path');
         
        if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageRegion) {
            exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
        }
        if (self::$auth == null) {
            self::$auth = new Client([
                'region' => self::$storageRegion,
                'credentials' => [
                    'secretId' => self::$accessKey,
                    'secretKey' => self::$secretKey,
                ]
            ]);
        }
        return self::$auth;
    }

    /**
     * TODO 文件上传 名称
     * @param string $filename
     * @return string
     */
    public static function uploadImage($filename = 'image')
    {
        $request = app('request');
        $file = $request->file($filename);
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
        try {
            self::autoInfo();
            $save_name  = self::$static_path.'/'.date('Ymd').'/'.$key;
            return [self::$uploadUrl . $key, self::$auth->putObject([
                'Bucket' => self::$storageName,
                'Key' => $save_name,
                'Body' => fopen($filePath, 'rb')
            ])];
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * TODO 文件上传 内容
     * @param $key
     * @param $content
     * @return string
     */
    public static function uploadImageStream($key, $content)
    {
        try {
            self::autoInfo();
            return [self::$uploadUrl . $key, self::$auth->putObject([
                'Bucket' => self::$storageName,
                'Key' => $key,
                'Body' => $content
            ])];
        } catch (\Exception $e) {
            return [false, $e->getMessage()];
        }
    }

    /**
     * TODO 删除资源
     * @param $key
     * @return mixed
     */
    public static function delete($key)
    {
        try {
            self::autoInfo();
            return self::$auth->deleteObject([
                'Bucket' => self::$storageName,
                'Key' => $key
            ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * TODO 转为文件流
     * @param $resource
     * @return EntityBody
     */
    public static function resourceStream($resource)
    {
        return EntityBody::factory($resource)->__toString();
    }

}